package com.kevin.zhangchao.scrollertest;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Interpolator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Scroller;

/**
 * Created by ZhangChao on 2016/12/18.
 */

public class CurtainView extends RelativeLayout implements View.OnTouchListener {
    private Context mContext;
    private Scroller mScroller;
    private int mScreenHeight;
    private int mScreenWidth;
    private ImageView img_curtain_ad;
    private ImageView img_curtain_rope;
    public int curtainHeight;
    private boolean isMove=false;
    private int downY;
    private int moveY;
    private int scrollY;
    private boolean isOpen=false;
    private int upY;
    /** 上升动画时间 */
    private int upDuration = 1000;
    /** 下落动画时间 */
    private int downDuration = 500;
    private static final String TAG = "CurtainView";

    public CurtainView(Context context) {
        super(context);
        init(context);
    }

    public CurtainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public CurtainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        this.mContext=context;
        BounceInterpolator interpolator=new BounceInterpolator();
        mScroller=new Scroller(context,interpolator);
        mScreenHeight=BaseTools.getWindowHeigh(context);
        mScreenWidth=BaseTools.getWindowWidth(context);
        this.setBackgroundColor(Color.argb(0,0,0,0));
        View view= LayoutInflater.from(context).inflate(R.layout.curtain,null);
        img_curtain_ad=(ImageView)view.findViewById(R.id.img_curtain_ad);
        img_curtain_rope=(ImageView)view.findViewById(R.id.img_curtain_rope);
        addView(view);
        img_curtain_ad.post(new Runnable(){

            @Override
            public void run() {
                curtainHeight=img_curtain_ad.getHeight();
                CurtainView.this.scrollTo(0,curtainHeight);
            }
        });

        img_curtain_rope.setOnTouchListener(this);


    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!isMove){
            int offViewY=0;
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    downY=(int)event.getRawY();
                    offViewY=downY-(int)event.getX();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    moveY=(int)event.getRawY();
                    this.scrollY=moveY-downY;
                    if (this.scrollY<0){
                        if (isOpen){
                            if (Math.abs(this.scrollY)<=img_curtain_ad.getBottom()-offViewY){
                                scrollTo(0,-this.scrollY);
                            }
                        }
                    }else{
                        if (!isOpen){
                            if (this.scrollY<=curtainHeight){
                                scrollTo(0,curtainHeight-this.scrollY);
                            }
                        }
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    upY=(int)event.getRawY();
                    if (Math.abs(upY-downY)<10){
                        onRopeClick();
                        break;
                    }
                    if (isOpen){
                        if (Math.abs(scrollY)>curtainHeight/2){
                            startMoveAnim(this.getScrollY(),
                                    (curtainHeight-this.getScrollY()),upDuration);
                            isOpen=false;
                        }else{
                            startMoveAnim(this.getScrollY(),
                                    -this.getScrollY(),upDuration);
                            isOpen=true;
                        }
                    }else{
                        if (scrollY>curtainHeight/2){
                            startMoveAnim(this.getScrollY(),
                                    -this.getScrollY(),upDuration);
                            isOpen=true;
                        }else{
                            startMoveAnim(this.getScrollY(),
                                    (curtainHeight-this.getScrollY()),upDuration);
                            isOpen=false;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void onRopeClick() {
        if(isOpen){
            CurtainView.this.startMoveAnim(0, curtainHeight, upDuration);
        }else{
            CurtainView.this.startMoveAnim(curtainHeight,-curtainHeight, downDuration);
        }
        isOpen = !isOpen;
    }

    private void startMoveAnim(int startY, int dy, int duration) {
        isMove=true;
        mScroller.startScroll(0,startY,0,dy,duration);
        invalidate();
    }


    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            Log.d(TAG, "computeScroll: getCurrX="+mScroller.getCurrX()+":getCurrY="+mScroller.getCurrX());
            postInvalidate();
            isMove=true;
        }else{
            isMove=false;
        }
        super.computeScroll();
    }
}
